﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Configuration;

namespace WDA.Core
{
	/// <summary>
	/// Enables logging functionality if configured.
	/// </summary>
	public class Logger
	{
		private static readonly bool _enabled;
		private readonly string _location;

		/// <summary>
		/// Creates new <see cref="Logger"/> instance for particular location.
		/// </summary>
		/// <param name="location">Location description.</param>
		public Logger(string location)
		{
			_location = location;
		}

      /// <summary>
      /// Creates new <see cref="Logger"/> instance for particular class.
      /// </summary>
      /// <param name="location">CLR type.</param>
      public Logger(Type location)
      {
         _location = location.FullName;
      }

		static Logger()
		{
			_enabled = ConfigurationManager.GetSection("loggingConfiguration") != null;			
		}

		/// <summary>
		/// Writes piece logging information.
		/// </summary>
		/// <param name="text">Message text.</param>
		/// <param name="a_arrFormatArgs">Formatting function arguments (as for <see cref="string.Format(string,object[])"/>).</param>
		public void WriteInfo(string text, params object[] a_arrFormatArgs)
		{
			Write(_location, text, null, TraceEventType.Information, null, a_arrFormatArgs);
		}

		/// <summary>
      /// Writes piece logging information.
		/// </summary>
		/// <param name="a_strLocation">Localization description of logging source (e.g. class name, namespace).</param>
		/// <param name="a_strText">Message text.</param>
		/// <param name="a_ex">Exception (if any).</param>
		/// <param name="a_eType">Event type.</param>
		/// <param name="a_strCategory">Category name.</param>
      /// <param name="a_arrFormatArgs">Formatting function arguments (as for <see cref="string.Format(string,object[])"/>).</param>
		public static void Write(string a_strLocation, string a_strText, Exception a_ex, TraceEventType a_eType, string a_strCategory, params object[] a_arrFormatArgs)
		{
			if (_enabled)
			{
				Microsoft.Practices.EnterpriseLibrary.Logging.LogEntry le = new Microsoft.Practices.EnterpriseLibrary.Logging.LogEntry();
				le.Message = string.Format(a_strText, a_arrFormatArgs);
				le.ExtendedProperties = new Dictionary<string, object>();
				le.ExtendedProperties["Location"] = a_strLocation;
				if (a_strCategory != null)
				{
					le.Categories.Add(a_strCategory);
				}
				if (a_ex != null)
				{
					le.Categories.Add("HandledException");
					le.ExtendedProperties["Message"] = a_ex.Message;
					le.ExtendedProperties["StackTrace"] = a_ex.StackTrace;
					foreach (object oKey in a_ex.Data.Keys)
					{
						le.ExtendedProperties[oKey.ToString()] = a_ex.Data[oKey].ToString();
					}
				}
				else if (a_strCategory == null)
				{
					le.Categories.Add("Information");
				}
				Microsoft.Practices.EnterpriseLibrary.Logging.Logger.Write(le);
			}
		}
	}
}
